package calendar;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

/* this class provides a panel to display the connection status */
class StatusPanel extends CalendarPanel implements ItemListener {

  private static final long serialVersionUID = 34734783L;

  String userName;
  JLabel statusLabel;
  JCheckBox ignoreDatesBox;
  boolean ignoreDates;
  boolean isConnected;

  public StatusPanel()
  {
    statusLabel = new JLabel( "Not Connected" );
    statusLabel.setBackground( Color.RED );

    isConnected = false;
    ignoreDates = false;
    ignoreDatesBox = new JCheckBox( "[ignore date filter]" );
    ignoreDatesBox.setSelected(true);
    this.ignoreDates = true;
    ignoreDatesBox.addItemListener( this );

    setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) );

    add( Box.createHorizontalStrut( 5 ) );
    add( statusLabel );
  }

  public void setConnectName( String name )
  {
    this.removeAll();
    statusLabel.setText( "Connected to: " + name + "'s calendar.  " +
      "(Hint: Start and End dates filter the event list)");
    statusLabel.setBackground( Color.GREEN );
    ignoreDatesBox.setBackground( Color.GREEN );

    this.add( statusLabel );
    this.add( ignoreDatesBox );

    updateUI();
  }

  public void setReconnectName( String name )
  {
    this.removeAll();
    statusLabel.setText( "Lost connection!  Attempting to reconnect to "
      + name + "'s calendar.  Please wait ..." );
    statusLabel.setBackground( Color.YELLOW );

    this.add( statusLabel );

    updateUI();
  }

  public void setDisconnected()
  {
    statusLabel.setText( "Not Connected" );
    this.remove( ignoreDatesBox );

    statusLabel.setBackground( Color.RED );
    ignoreDatesBox.setBackground( Color.RED );

    updateUI();
  }
  
  public boolean getIgnoreDates()
  {
    return this.ignoreDates;
  }

  /* check if the user selected the ignore dates option */
  public void itemStateChanged( ItemEvent e )
  {
    if ( e.getStateChange() == ItemEvent.SELECTED )
    {
      this.ignoreDates = true;
    }
    if ( e.getStateChange() == ItemEvent.DESELECTED )
    {
      this.ignoreDates = false;
    }

    int eventId = CalEvent.LIST_EVENTS;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }
}
